import { Component, Input, Output, EventEmitter, OnInit, OnChanges } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { HttpServcie } from '../../../../service/http.service';
import { RoleTypeBean } from './../bean/role-type.bean';
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import {Observable} from 'rxjs/Rx';
import { Config } from '../../../../service/config';

@Component({
  selector: 'role-type-add-page',
  templateUrl: 'role-type.add.html',
  styleUrls: ['role-type.add.scss']
})
export class RoleTypeAddPage implements OnInit, OnChanges {
  validateForm: FormGroup;
  isSpinning: boolean=false;
  functions: any = [];
  expandDataCache = {};
  @Input() inValue: any = [];
  data=[];
  @Output("outPut") outPut = new EventEmitter()
  //角色信息
  roleTypeBean:RoleTypeBean=new RoleTypeBean();
  config:any=Config;
  partyList:any=[];
  roleList:any=[];
  constructor(
    private fb: FormBuilder,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService
  ) {

  }

  ngOnInit() {

    this.validateForm = this.fb.group({
      roleTypeName: [null, [Validators.required]],
      roleTypeDescribe: [null, [Validators.required]],
      roleList: [null, [Validators.required]],
      partyList: [null, [Validators.required]],
    });
    this.partyList = this.config.PARTY_TYPE;
    this.roleList=[];
    this.httpServcie.roleQueryListAll({}).subscribe(data=>{
      if(data.status==200){
        this.roleList = data.data;
      }else{
        this._notification.create('error', '错误提示', data.message);
      }
    })

  }

  ngOnChanges() {
    this.functions = [];
    if (this.inValue) {
      this.functions = this.inValue;
    }
  }

  submitForm() {
    //校验是否能提交
    //没有填的标红
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if(!this.validateForm.controls[i].valid){
        return;
      }
    }

    this.isSpinning = true;
    let ob = this.httpServcie.systemRoleTypeAdd(this.roleTypeBean).subscribe(data=>{
      this.isSpinning = false;
      if(data.status==200){
        this._notification.create('success', '成功提示', '新增成功');
        this.outPut.emit(data);
      }else{
        this._notification.create('error', '错误提示', data.message);
      }

    })

  }

  //交验
  getFormControl(name) {
    return this.validateForm.controls[name];
  }

}
